<?php

namespace common\libraries;

use Yii;

/**
 *
 */
class ResourceLoader
{

    /**
     * @return mixed|string
     */
    public static function getVersion()
    {
        return isset(Yii::$app->params['res']) ? Yii::$app->params['res']['version'] : '';
    }

    public static function getBaseurl()
    {
        return isset(Yii::$app->params['res']) ? Yii::$app->params['res']['baseurl'] : '/';
    }

    /**
     *
     * @param array $files
     * @return string
     */
    public static function includeCss(array $files)
    {
        $str = '';
        foreach ($files as $f) {
            $operator = strpos($f, '?') === false ? '?' : '&';
            $path = self::getBaseurl() . $f . $operator . self::getVersion();
            $str .= '<link rel="stylesheet" type="text/css" href="' . $path . '" />';
        }
        echo $str;
    }

    /**
     *
     * @param array $files
     * @return string
     */
    public static function includeJs(array $files)
    {
        $str = '';
        foreach ($files as $f) {
            $operator = strpos($f, '?') === false ? '?' : '&';
            $path = self::getBaseurl() . $f . $operator . self::getVersion();
            $str .= '<script type="text/javascript" src="' . $path . '"></script>';
        }
        echo $str;
    }

    public static function formatUrl($url)
    {
        if (empty($url)) {
            return '';
        }
        return strpos($url, 'http') !== 0 ? Yii::$app->params['upload']['host'] . $url : $url;
    }

}
